﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BobDotNet.Classes;
using System.Runtime.Remoting.Messaging;
using BobDotNet.Classes.Application;
using BobDotNet.Forms;

namespace BobDotNet
{
    public partial class MainControl : Form
    {
        public bool Start;

        public MainControl()
        {
            log4net.Config.XmlConfigurator.Configure();
            InitializeComponent();

            Start = Conn();
            if (Start)
                LanceMenu();
        }

        private void LanceMenu()
        {
            AccesRapide ar = new AccesRapide();
            ar.MdiParent = this;
            ar.Show();
            ar.Location = new Point(0, 0);

            MajStat();
        }

        private bool Conn()
        {
            BobDotNet.Forms.Tools.Connexion c = new Forms.Tools.Connexion();
            bool ret = c.Montre();
            if (ret)
            {
                tsUser.Text = ((User)CallContext.GetData(KEY.USER_CONNECTED_KEY)).Login;
                tsPc.Text = Environment.MachineName;
            }
            return ret;
        }

        private void MajStat()
        {
            
            /*ISession session = sf.OpenSession();
            try
            {
                StatDi sd = (StatDi)session.CreateCriteria(typeof(StatDi)).Add(Expression.Eq("Jour", DateTime.Today)).UniqueResult();
                session.Close();
                if (sd == null)
                {
                    sd = new StatDi();
                    sd.Jour = DateTime.Today;
                }
                session = sf.OpenSession();
                List<Di> ls = (List<Di>)session.CreateCriteria(typeof(Di)).List<Di>();
                sd.NbTotDi = ls.Count();
                sd.NbDiEnAttente = ls.Count(p => p.EtatDi.Id == 1);
                sd.NbDiEnCours = ls.Count(p => p.EtatDi.Id == 2);
                sd.NbDiTransmise = ls.Count(p => p.EtatDi.Id == 3);
                sd.NbDiEnValidation = ls.Count(p => p.EtatDi.Id == 4);
                sd.NbDiValidee = ls.Count(p => p.EtatDi.Id == 5);

                if (sd.Id.Equals(0))
                    session.Save(sd);
                else
                    session.Update(sd);

                if (session.IsDirty())
                {
                    session.Flush();
                    session.Clear();
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + " " + ex.StackTrace, "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                if (session.IsConnected)
                    session.Close();
            }*/
        }
    }
}
